#!/bin/bash

set -e # exit on error
set -x # echo commands

git fetch origin master

MINICONDA="Miniconda-$MINICONDA_VERSION-Linux-x86_64"
MINICONDA_URL="http://repo.continuum.io/miniconda/$MINICONDA.sh"

PINNED_PKGS=$(cat <<EOF
EOF
)

wget $MINICONDA_URL
bash $MINICONDA.sh -b -f -p $HOME/miniconda
rm -rf $MINICONDA.sh

python -V

echo -e "$PINNED_PKGS" > /home/travis/miniconda/conda-meta/pinned

DEPS_TRAVIS="python=$TRAVIS_PYTHON_VERSION conda=4.1.11 conda-env=2.5.2 conda-build=1.21.14"
conda install --yes $DEPS_TRAVIS

conda config --append channels bokeh
conda config --append channels conda-forge
conda config --get channels

CONDA_PY="${TRAVIS_PYTHON_VERSION/./}" conda build --quiet --no-test conda.recipe

NOARCH_DIR="$HOME/miniconda/conda-bld/noarch"
echo "noarch build output contents:"
ls -l $NOARCH_DIR

BUILD_FILE_COUNT="$(ls $NOARCH_DIR | wc -l)"
if [ $BUILD_FILE_COUNT != 3 ]; then
   echo "noarch build has wrong number of files"
   exit 1
fi

conda install bokeh --use-local --yes

DEPS_RUN=$(cat <<EOF | python -
from conda_build.metadata import MetaData
print(" ".join([s.replace(" ", "") for s in MetaData("conda.recipe").get_value("requirements/run")]))
EOF
)

DEPS_TEST=$(cat <<EOF | python -
from conda_build.metadata import MetaData
print(" ".join([s.replace(" ", "") for s in MetaData("conda.recipe").get_value("test/requires")]))
EOF
)

conda install --yes $DEPS_RUN
conda install --yes $DEPS_TEST

conda install --yes nodejs
echo "node version $(node -v)"
echo "npm version $(npm -v)"

MATPLOTLIB_RC=$(python -c "import matplotlib; print(matplotlib.matplotlib_fname())")
sed -i 's/^backend\s*:.*$/backend: agg/' $MATPLOTLIB_RC

python -c 'import bokeh; bokeh.sampledata.download(progress=False)'
